package cn.com.controler.SystemState;
import cn.com.controler.Util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class SystemStateDAOTemplete {

    //添加数据
    public static int insertdata(String str,Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,null);
        }
        return 0;
    }


    //查询数据
    public static int selectdata(String str, Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            resultSet = statement.executeQuery();
            if(resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,resultSet);
        }
        return 0;
    }
}
